/**
 * 管理初始化
 */
var InsuranceKind = {
    id: "InsuranceKindTable",	            //表格id
    policyId: $("#policyId").val(),	        //保单ID
    seItem: null,		                    //选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
InsuranceKind.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '', field: 'able_edit', visible: false, align: 'center', valign: 'middle'},
            {title: '险种代码', field: 'insurance_kind_code', visible: true, align: 'center', valign: 'middle'},
            {title: '险种名称', field: 'insurance_kind_name', visible: true, align: 'center', valign: 'middle'},
            {title: '险种类型', field: 'insuranceKindTypeName', visible: true, align: 'center', valign: 'middle'},
            {title: '固定保额（元）', field: 'fixAmountLimit', visible: true, align: 'center', valign: 'middle',
                formatter: function (value) {
                    return Feng.numberFormat(value);
                }},
            {title: '保费-金额（元）', field: 'fixAmount', visible: true, align: 'center', valign: 'middle',
                formatter: function (value) {
                    return Feng.numberFormat(value);
                }},
            {title: '浮动保额免核保额度（元）', field: 'floatAmountLimit', visible: true, align: 'center', valign: 'middle',
                formatter: function (value) {
                    return Feng.numberFormat(value);
                }},
            {title: '保费-比例（%）', field: 'floatAmount', visible: true, align: 'center', valign: 'middle',
                formatter: function (value) {
                    return Feng.numberFormat(value);
                }}
    ];
};

/**
 * 检查是否选中
 */
InsuranceKind.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        InsuranceKind.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加
 */
InsuranceKind.openAddInsuranceKind = function () {
    var index = layer.open({
        type: 2,
        title: '添加险种',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/insuranceKind/insuranceKind_add/' + InsuranceKind.policyId
    });
    this.layerIndex = index;
};

/**
 * 打开查看详情
 */
InsuranceKind.openInsuranceKindDetail = function (status) {
    if (this.check()) {
        if(status == 1 ){
            if(InsuranceKind.seItem.able_edit > 0){
                Feng.error("存在关联的保障计划，险种不能修改。");
                return;
            }
        }
        var index = layer.open({
            type: 2,
            title: '详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/insuranceKind/insuranceKind_update/' + InsuranceKind.seItem.id +'/' +status
        });
        this.layerIndex = index;
    }
};

/**
 * 删除
 */
InsuranceKind.delete = function () {
    if (this.check()) {
        if(InsuranceKind.seItem.able_edit > 0){
            Feng.error("存在关联的保障计划，险种不能删除。");
            return;
        }
        Feng.confirm("是否删除险种 "+this.seItem.insurance_kind_name+"（"+ this.seItem.insurance_kind_code +"）?",function () {
            var ajax = new $ax(Feng.ctxPath + "/insuranceKind/delete", function (data) {
                Feng.success("删除成功!");
                InsuranceKind.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("insuranceKindId",InsuranceKind.seItem.id);
            ajax.start();
        })

    }
};

$(function () {
    var defaultColunms = InsuranceKind.initColumn();
    var table = new BSTable(InsuranceKind.id, "/insuranceKind/list/"+InsuranceKind.policyId, defaultColunms);
    table.setPaginationType("client");
    InsuranceKind.table = table.init();
});
